package com.ng.desktop.view.categoria;

import javax.swing.JOptionPane;

import com.ng.desktop.model.Alimento;
import com.ng.desktop.model.Categoria;
import com.ng.desktop.persistence.AlimentoPersistence;
import com.ng.desktop.persistence.CategoriaPersistence;
import com.ng.desktop.services.NGFactoryBean;
import com.ng.framework.persistence.NGSearchCriteria;
import com.ng.framework.persistence.NGSearchParameter;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.view.desktop.NGTablePanel;

public class CategoriaTablePanel extends
		NGTablePanel<Categoria, CategoriaPersistence> {

	private AlimentoPersistence alimentoPersistence;

	public CategoriaTablePanel(CategoriaPersistence persistence) {
		super(persistence);
	}

	private static final long serialVersionUID = -3041915458364809210L;

	private static final String[] COLUMN_NAMES = { "Id", "Nombre", "Energía",
			"CHON", "CHO", "FAT" };

	private static final int[] COLUMN_WIDTHS = { 70, 205, 70, 70, 70, 70 };

	@Override
	protected String[] getColumnNames() {
		return COLUMN_NAMES;
	}

	@Override
	protected int[] getColumnWidths() {
		return COLUMN_WIDTHS;
	}

	@Override
	protected Object[] toRow(Categoria categoria) {
		return new Object[] { categoria.getId(), categoria.getNombre(),
				categoria.getEnergia(), categoria.getChon(),
				categoria.getCho(), categoria.getFat() };
	}

	@Override
	protected boolean create(CategoriaPersistence persistence) {
		CategoriaEditDialog dialog = new CategoriaEditDialog(
				JOptionPane.getFrameForComponent(this), persistence);
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);
		return true;
	}

	@Override
	protected boolean edit(CategoriaPersistence persistence, Categoria instance) {
		CategoriaEditDialog dialog = new CategoriaEditDialog(
				JOptionPane.getFrameForComponent(this), persistence, instance);
		dialog.setLocationRelativeTo(this);
		dialog.setVisible(true);
		return true;
	}

	@Override
	protected Categoria getSelectedInstance(CategoriaPersistence persistence,
			Object[] rowData) {
		return persistence.findById((Long) rowData[0]);
	}

	@Override
	protected boolean esBorrable(Categoria categoria) {
		NGSearchCriteria searchCriteria = new NGSearchCriteria();
		searchCriteria.addNGSearchParameter(new NGSearchParameter(
				Alimento.class, "categoria", String.valueOf(categoria.getId()),
				Type.INTEGER, UnaryOperation.EQUALS, true, "id"));

		boolean borrar = this.getAlimentoPersistence().getAll(searchCriteria)
				.size() == 0;

		if(!borrar) {
			JOptionPane
			.showMessageDialog(
					JOptionPane.getFrameForComponent(this),
					"La categoría seleccionada no se puede borrar ya que se encuentra asociada a uno o mas alimentos.",
					"Error", JOptionPane.ERROR_MESSAGE);			
		}

		return borrar;
	}

	public AlimentoPersistence getAlimentoPersistence() {
		if (alimentoPersistence == null) {
			alimentoPersistence = (AlimentoPersistence) NGFactoryBean
					.getInstance().getBean("alimentoPersistence");
		}

		return alimentoPersistence;
	}
}
